package rhinestone.Model;

import java.util.HashMap;
import java.io.Serializable;

/**
 * The container of orders.
 *
 * @author Group 2
 * @version 2010.12.21.
 */
public class OrderContainer implements Serializable {
	private HashMap<Integer, Order> pendingOrders;
	private HashMap<Integer, Order> orderHistory;
	private static OrderContainer instance;
	private boolean changed;

	/**
	 * Private constructor which initializes instance variables.
	 */
	private OrderContainer() {
		pendingOrders = new HashMap<Integer, Order>();
		orderHistory = new HashMap<Integer, Order>();
		changed = false;
	}

	/**
	 * Singleton pattern.
	 *
	 * @return The object of this container.
	 */
	public static OrderContainer getInstance() {
		if(instance == null)
			instance = new OrderContainer();
		return instance;
	}

	public void addOrder(Order order) {
		if(getOrder(order.getId()) == null) {
			pendingOrders.put(order.getId(), order);
			changed = true;
		}
		else
			System.err.println("Order with ID " + order.getId() + " already exists.");
	}

	public Order getOrder(int id) {
		Order order = pendingOrders.get(id);
		if(order == null)
			order = orderHistory.get(id);
		return order;
	}

	public boolean isChanged() {
		return changed;
	}

	public void setChanged(boolean changed) {
		this.changed = changed;
	}
}